package com.city.java.reports.service.impl;

import com.city.java.base.utils.PageVO;
import com.city.java.reports.mapper.ReportMapper;
import com.city.java.reports.pojo.dto.ReportQuery;
import com.city.java.reports.pojo.entity.Reports;
import com.city.java.base.response.JsonResult;
import com.city.java.base.response.StatusCode;
import com.city.java.reports.pojo.vo.ReportVO;
import com.city.java.reports.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 举报信息服务实现类
 */
@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    public JsonResult addReport(Reports report) {
        log.debug("准备添加举报信息: {}", report);

        // 参数校验
        if (report == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报信息不能为空");
        }

        if (report.getReporterId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报人ID不能为空");
        }

        if (report.getReportType() == null || report.getReportType().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报类型不能为空");
        }

        if (report.getReason() == null || report.getReason().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报原因不能为空");
        }

        // 设置创建时间
        if (report.getCreatedAt() == null) {
            report.setCreatedAt(LocalDateTime.now());
        }

        try {
            reportMapper.insertReport(report);
            log.info("举报信息添加成功: 举报ID={}", report.getReportId());
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("添加举报信息失败: 举报人ID={}, 举报类型={}", report.getReporterId(), report.getReportType(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "添加举报信息失败");
        }
    }

    public JsonResult getReportById(Long reportId) {
        log.debug("根据ID查询举报信息: {}", reportId);

        // 参数校验
        if (reportId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报ID不能为空");
        }

        try {
            Reports report = reportMapper.selectById(reportId);
            if (report != null) {
                log.info("查询举报信息成功: ID={}", reportId);
                return JsonResult.ok(report);
            } else {
                log.warn("举报信息不存在: ID={}", reportId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报信息不存在");
            }
        } catch (Exception e) {
            log.error("查询举报信息失败: ID={}", reportId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报信息失败");
        }
    }

    public JsonResult getReportsByReporterId(Long reporterId) {
        log.debug("根据举报人ID查询举报信息列表: {}", reporterId);

        // 参数校验
        if (reporterId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报人ID不能为空");
        }

        try {
            List<Reports> reports = reportMapper.selectByReporterId(reporterId);
            log.info("查询举报人举报信息列表成功: 举报人ID={}, 举报数量={}", reporterId, reports.size());
            return JsonResult.ok(reports);
        } catch (Exception e) {
            log.error("查询举报人举报信息列表失败: 举报人ID={}", reporterId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报信息列表失败");
        }
    }

    public JsonResult getReportsByReportType(String reportType) {
        log.debug("根据举报类型查询举报信息列表: {}", reportType);

        // 参数校验
        if (reportType == null || reportType.trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报类型不能为空");
        }

        try {
            List<Reports> reports = reportMapper.selectByReportType(reportType);
            log.info("查询举报类型信息列表成功: 举报类型={}, 举报数量={}", reportType, reports.size());
            return JsonResult.ok(reports);
        } catch (Exception e) {
            log.error("查询举报类型信息列表失败: 举报类型={}", reportType, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报信息列表失败");
        }
    }

    @Override
    public PageVO<ReportVO> select(ReportQuery reportQuery) {
        // 计算分页偏移量
        if (reportQuery.getPageNum() != null && reportQuery.getPageSize() != null) {
            reportQuery.setPageNum((reportQuery.getPageNum() - 1) * reportQuery.getPageSize());
        }

        List<ReportVO> list = reportMapper.select(reportQuery);
        Long total = reportMapper.getTotalCount(reportQuery);

        PageVO<ReportVO> pageVO = new PageVO<>();
        pageVO.setList(list);
        pageVO.setTotalPage(total);

        log.debug("查询举报列表成功: {}", list);
        return pageVO;
    }

    @Override
    public JsonResult deleteReportById(Long reportId) {
        log.debug("根据ID删除举报信息: {}", reportId);

        // 参数校验
        if (reportId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报ID不能为空");
        }

        try {
            int result = reportMapper.deleteById(reportId);
            if (result > 0) {
                log.info("删除举报信息成功: ID={}", reportId);
                return JsonResult.ok();
            } else {
                log.warn("删除举报信息失败，举报信息不存在: ID={}", reportId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报信息不存在");
            }
        } catch (Exception e) {
            log.error("删除举报信息失败: ID={}", reportId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除举报信息失败");
        }
    }

    @Override
    public JsonResult updateReport(Reports report) {
        log.debug("更新举报信息: {}", report);

        // 参数校验
        if (report == null || report.getReportId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报ID不能为空");
        }

        if (report.getReportType() != null && report.getReportType().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报类型不能为空");
        }

        if (report.getReason() != null && report.getReason().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报原因不能为空");
        }

        try {
            // 检查举报信息是否存在
            Reports existingReport = reportMapper.selectById(report.getReportId());
            if (existingReport == null) {
                log.warn("更新举报信息失败，举报信息不存在: ID={}", report.getReportId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报信息不存在");
            }

            int result = reportMapper.updateById(report);
            if (result > 0) {
                log.info("更新举报信息成功: ID={}", report.getReportId());
                return JsonResult.ok();
            } else {
                log.warn("更新举报信息失败: ID={}", report.getReportId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "更新举报信息失败");
            }
        } catch (Exception e) {
            log.error("更新举报信息失败: ID={}", report.getReportId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "更新举报信息失败");
        }
    }
}
